package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hpplay.sdk.source.api.LelinkPlayerInfo;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.stark.cast.screen.CastScreenManager;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelAudioAdapter;
import flc.ast.databinding.ActivityProAudioBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.MediaLoader;
import stark.common.basic.media.bean.AudioBean;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.UriUtil;

public class ProAudioActivity extends BaseAc<ActivityProAudioBinding> {

    private SelAudioAdapter audioAdapter;
    private CastScreenManager mCastScreenManager;
    private int oldPosition = 0;
    private String selPath = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_pro_audio;
    }

    @Override
    protected void initView() {
        mDataBinding.ivProAudioBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        mDataBinding.rvAudioList.setLayoutManager(gridLayoutManager);
        audioAdapter = new SelAudioAdapter();
        audioAdapter.setGridLayoutManager(gridLayoutManager);
        audioAdapter.setEdit(true);
        audioAdapter.setNoManage(true);
        mDataBinding.rvAudioList.setAdapter(audioAdapter);
        audioAdapter.setOnItemClickListener(this);

        mDataBinding.tvStartPro.setOnClickListener(this);
        //初始化投屏
        mCastScreenManager = CastScreenManager.getInstance();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvStartPro:
                if (TextUtils.isEmpty(selPath)) {
                    ToastUtils.showShort(getString(R.string.please_sel_audio));
                    return;
                }
                proAudio(selPath);
                ToastUtils.showShort(getString(R.string.pro_suc));
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        audioAdapter.getItem(oldPosition).setSelected(false);
        audioAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        audioAdapter.getItem(position).setSelected(true);
        audioAdapter.notifyItemChanged(position);
        selPath = audioAdapter.getItem(position).getPath();
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 1));
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        getPermission();
    }

    //音频投屏
    private void proAudio(String url) {
        LelinkPlayerInfo playerInfo = new LelinkPlayerInfo();
        playerInfo.setType(LelinkSourceSDK.MEDIA_TYPE_AUDIO);
        Uri uri = Uri.parse(url);
        if (UriUtil.isContentSchemeUri(uri)) {
            playerInfo.setLocalUri(uri);
        } else {
            playerInfo.setLocalPath(url);
        }
        mCastScreenManager.startPlay(playerInfo);
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_AUDIO)
                .reqPermissionDesc(getString(R.string.get_music_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        ToastUtils.showShort(R.string.no_permission);
                    }
                }).request();
    }

    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<AudioBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<AudioBean>> emitter) {
                List<AudioBean> audioBeans = MediaLoader.loadAudio();
                emitter.onNext(audioBeans);
            }

            @Override
            public void accept(List<AudioBean> audioBeanList) {
                if (audioBeanList != null) {
                    audioAdapter.setList(audioBeanList);
                    mDataBinding.rvAudioList.setVisibility(View.VISIBLE);
                    mDataBinding.ivProAudioNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvAudioList.setVisibility(View.GONE);
                    mDataBinding.ivProAudioNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}